package com.xpoint.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xpoint.backend.common.BaseContext;
import com.xpoint.backend.common.R;
import com.xpoint.backend.entity.AddressBook;
import com.xpoint.backend.mapper.UserAddressMapper;
import com.xpoint.backend.service.UserAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, AddressBook> implements UserAddressService {

    @Transactional
    @Override
    public R<String> insertAddressBook(AddressBook addressBook) {
//        通过线程获取当前用户id
        if (addressBook!=null){
            addressBook.setUserId(BaseContext.getCurrentId());
            if (this.save(addressBook)) {
                return R.success("添加成功");
            }else {
                return R.error("添加失败");
            }
        }

//        System.out.println(addressBook);
        return R.error("未知错误");
    }

    @Override
    public R<List<AddressBook>> findAddressBookList() {
//        用户的id
        Long currentId = BaseContext.getCurrentId();
        if (currentId!=null) {
//            创建条件构造器
            LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AddressBook::getUserId,currentId);
//            查询地址簿里面id=当前的。
            List<AddressBook> list = this.list(lambdaQueryWrapper);
            if (list!=null){
                return R.success(list);
            }else {
                return R.error("失败");
            }
        }
        return R.error("未知错误");
    }

    @Override
    public R<String> upDefault(Long id) {
//        设置默认地址

//        从线程获取用户的id
        Long currentId = BaseContext.getCurrentId();
        if (id!=null) {
//            先将当前用户所有的,默认地址设置成0
            LambdaUpdateWrapper<AddressBook> lambdaUpdateWrapper =new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId()).set(AddressBook::getIsDefault,0);
            boolean update = this.update(lambdaUpdateWrapper);
//            再根据传过来的地址id,更新默认地址
            AddressBook addressBook = new AddressBook();
            addressBook.setUserId(currentId);
            addressBook.setIsDefault(1);
            addressBook.setId(id);
            boolean b = this.updateById(addressBook);
            if (b){
                return R.success("设置成功");
            }else {
                return R.error("设置失败");
            }

        }
        return R.error("未知错误");
    }

    @Override
//    更新地址
    public R<AddressBook> upAddressBookById(Long id) {
        if (id!=null){
            AddressBook byId = this.getById(id);
            if (byId!=null){
                return R.success(byId);
            }
            return R.error("失败");
        }
        return R.error("未知错误");
    }

    @Override
//    删除地址
    public R<String> deleteAddressBookById(Long id) {
        if (this.removeById(id)) {
            return R.success("成功");
        }
        return  R.error("失败");
    }

    @Override
//    更新地址
    public R<String> updateAddressBook(AddressBook addressBook) {
        if (addressBook!=null) {
            if (this.updateById(addressBook)) {
                return R.success("成");
            }
        }
        return  R.error("失败");
    }

    @Override
//    获取默认地址
    public R<AddressBook> getDefaultAddress() {
//        获取用户的id
        Long currentId = BaseContext.getCurrentId();

//        通过用户的id获取默认的地址
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId,currentId);
//        条件：字段isdefault等于1
        lambdaQueryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook one = getOne(lambdaQueryWrapper);
        if (one!=null){
            return R.success(one);
        }
        return R.error("未知错误");
    }
}
